import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:ball_lion/entity/expert/plan_pack/plan_pack_entity.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/server/api/expert_api.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/widgets/asset_item.dart';
import 'package:ball_lion/widgets/base/wqs_text.dart';
import 'package:ball_lion/routes/routes.dart';
import 'package:ball_lion/widgets/circular.dart';

class PlanPackCard extends StatefulWidget {
  const PlanPackCard({super.key, required this.expertId, this.margin});

  final String expertId;
  final EdgeInsets? margin;

  @override
  State<PlanPackCard> createState() => _PlanPackCardState();
}

class _PlanPackCardState extends State<PlanPackCard> {
  List<PlanPackEntity>? planPack;
  int _current = 0;

  set current(int value) => setState(() => _current = value);

  @override
  void initState() {
    getData();
    super.initState();
  }

  void getData() async {
    List<PlanPackEntity> result =
        await ExpertApi.getPlanPack(expertId: widget.expertId);
    planPack = result;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return planPack.noValue
        ? Container()
        : Stack(
            children: [
              CarouselSlider(
                  options: CarouselOptions(
                    aspectRatio: 343 / 72,
                    scrollPhysics: const ClampingScrollPhysics(),
                    viewportFraction: 1,
                    enableInfiniteScroll:
                        (planPack?.length ?? 0) > 1 ? true : false,
                    autoPlay: true,
                    onPageChanged: (index, reason) => current = index,
                  ),
                  items: planPack?.map<Widget>((e) => item(e)).toList()),
              if ((planPack?.length ?? 0) > 1)
                Positioned(
                    bottom: 6,
                    right: 28,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: List<Widget>.generate(
                          planPack!.length,
                          (index) => AnimatedContainer(
                              duration: const Duration(milliseconds: 100),
                              width: _current == index ? 8 : 3,
                              height: 3,
                              margin: const EdgeInsets.symmetric(horizontal: 4),
                              decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(1.5),
                                  color: _current == index
                                      ? Colours.white
                                      : Colours.white.withOpacity(0.5)))),
                    ))
            ],
          );
  }

  Widget item(PlanPackEntity entity) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () =>
          Get.toNamed(Routes.planPackDetail, arguments: entity.packageId),
      child: Circular(
        radius: 8,
        margin: widget.margin,
        clipBehavior: Clip.antiAlias,
        child: Stack(
          children: [
            Positioned.fill(
                child: Image.asset(Utils.getImgPath("plan_pack_bg.png"),
                    fit: BoxFit.cover)),
            Padding(
              padding: const EdgeInsets.all(10),
              child: Row(
                children: [
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        const AssetItem(
                          asset: "plan_pack_icon.png",
                          name: "方案套餐",
                          assetSize: 12,
                          direction: AssetDirection.ltr,
                          textStyle: TextStyle(
                              fontSize: 14,
                              color: Colours.brown835A39,
                              fontWeight: FontWeight.w500),
                        ),
                        Container(height: 4),
                        Expanded(
                            child: QsText(entity.title ?? "",
                                fontSize: 11,
                                color: Colours.brown835A39,
                                maxLines: 2)),
                      ],
                    ),
                  ),
                  Circular(
                      width: 60,
                      height: 30,
                      radius: 18,
                      margin: const EdgeInsets.only(left: 10),
                      alignment: Alignment.center,
                      gradient: const LinearGradient(
                        colors: [Color(0xFFFFBA41), Color(0xFFFF9900)],
                      ),
                      child: QsText(
                        entity.isCanBuy == 1 ? "去购买" : "去查看",
                        fontSize: 13,
                        fontWeight: FontWeight.w500,
                        color: Colours.white,
                        needCenter: true,
                      ))
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
